<?php

namespace common\modules\article\models;

use common\behaviors\TimestampBehavior;
use common\components\ActiveRecord;
use Yii;
use yii\db\Expression;

/**
 * This is the model class for table "{{%article_item}}".
 *
 * @property integer $id
 * @property integer $category_id
 * @property string $title
 * @property string $image
 * @property string $description
 * @property string $text
 * @property string $slug
 * @property integer $views
 * @property string $status
 * @property integer $created_at
 * @property integer $updated_at
 * @property integer $created_by
 * @property integer $updated_by
 * @property Category $category
 */
class Item extends ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%article_item}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['category_id', 'views', 'created_by', 'updated_by'], 'integer'],
            [['title', 'text', 'slug'], 'required'],
            [['description', 'text'], 'string'],
            [['title', 'image', 'slug'], 'string', 'max' => 128],
            [['status'], 'string', 'max' => 100],
            [['slug'], 'unique']
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app/article', 'ID'),
            'category_id' => Yii::t('app/article', 'Category ID'),
            'title' => Yii::t('app/article', 'Title'),
            'image' => Yii::t('app/article', 'Main Image'),
            'description' => Yii::t('app/article', 'Description'),
            'text' => Yii::t('app/article', 'Text'),
            'slug' => Yii::t('app/article', 'Slug'),
            'views' => Yii::t('app/article', 'Views'),
            'status' => Yii::t('app/article', 'Status'),
            'created_at' => Yii::t('app/article', 'Created At'),
            'updated_at' => Yii::t('app/article', 'Updated At'),
            'created_by' => Yii::t('app/article', 'Created By'),
            'updated_by' => Yii::t('app/article', 'Updated By'),
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function behaviors()
    {
        $behaviors = parent::behaviors();

        $behaviors[] = [
            'class' => TimestampBehavior::className(),
            'createdAtAttribute' => 'created_at',
            'updatedAtAttribute' => 'updated_at',
            'value' => new Expression('NOW()'),
        ];
        
        return $behaviors;
    }


    /**
     * @return \yii\db\ActiveQuery
     */
    public function getCategory()
    {
        return $this->hasOne(Category::className(), ['id' => 'category_id']);
    }

        /**
     * status对应的工作流列表
     *
     * @return array
     */
    public function statusWorkflow()
    {
        // 简单的流程定义
        return [
            'initStatus' => 'draft',
            'startStatus' => '开始',
            'endStatus' => '结束',
            'status' => [
                'draft' => [
                    'label' => '草稿',
                    'value' => 'draft',
                    'next' => 'audit',
                ],
                'audit' => [
                    'label' => '审核中',
                    'value' => 'audit',
                    'direction' => 'right',
                    'next' => [
                        'true' => ['bottom', 'publish'],
                        'false' => 'draft',
                    ],
                ],
                'publish' => [
                    'label' => '发布中',
                    'value' => 'publish',
                    'direction' => 'right',
                    'next' => [
                        'true' => 'publish_success',
                        'false' => 'publish_failed',
                    ],
                ],
                'publish_failed' => [
                    'label' => '发布失败',
                    'value' => 'publish_failed',
                    'direction' => 'left',
                    'next' => 'draft',
                ],
                'publish_success' => [
                    'label' => '发布成功',
                    'value' => 'publish_success',
                    //'direction' => 'right',
                    'next' => 'archived',
                ],
                'archived' => [
                    'label' => '存档',
                    'value' => 'archived',
                    'direction' => 'right',
                    'next' => '_end',
                ],
            ],
        ];
    }
                
}
